package com.chain.qs.modules.biz.service;

import com.chain.qs.common.service.CrudService;
import com.chain.qs.modules.biz.dao.EquipmentItemWarningRuleDao;
import com.chain.qs.modules.biz.entity.EquipmentItemWarningRule;
import com.chain.qs.modules.biz.entity.EventTemplateItem;
import com.chain.qs.modules.biz.entity.WarningRecord;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//@Transactional(readOnly = true)
public class EquipmentItemWarningRuleService extends CrudService<EquipmentItemWarningRuleDao, EquipmentItemWarningRule> {

    @Resource
    private EventTemplateItemService eventTemplateItemService;
    @Resource
    private WarningRecordService warningRecordService;


    List<EquipmentItemWarningRule> findListByParamId(String findListByParamId) {
        return dao.findListByParamId(findListByParamId);
    }
    //模板添加规则
    public void saveEquipmentItemWarningRuleList(String eventTemplateId, String itemId, String sgroupId) {
        //1、删除规则表中该站组下设备已存在的规则
        EquipmentItemWarningRule equipmentItemWarningRule = new EquipmentItemWarningRule();
        equipmentItemWarningRule.setItemId(itemId);
        equipmentItemWarningRule.setSgroupId(sgroupId);
        dao.deleteByStationGroupEquItem(equipmentItemWarningRule);
        //2、获取选定模板的所有规则
        List<EventTemplateItem> eventTemplateItemList = eventTemplateItemService.findByTemplateId(eventTemplateId);
        //3、将模板中的规则逐条加入到规则表中
        for (EventTemplateItem eventTemplateItem : eventTemplateItemList) {
            equipmentItemWarningRule = new EquipmentItemWarningRule();
            equipmentItemWarningRule.setParam(eventTemplateItem.getParam());
            equipmentItemWarningRule.setItemId(itemId);
            equipmentItemWarningRule.setSgroupId(sgroupId);
            equipmentItemWarningRule.setComparisonOperator1(eventTemplateItem.getComparisonOperator1());
            equipmentItemWarningRule.setOperand1(eventTemplateItem.getOperand1());
            equipmentItemWarningRule.setRelationOperator(eventTemplateItem.getRelationOperator());
            if (eventTemplateItem.getComparisonOperator2() != null && !eventTemplateItem.getComparisonOperator2().equals("")) {
                equipmentItemWarningRule.setComparisonOperator2(eventTemplateItem.getComparisonOperator2());
            }
            if (eventTemplateItem.getOperand2() != null && !eventTemplateItem.getOperand2().equals("")) {
                equipmentItemWarningRule.setOperand2(eventTemplateItem.getOperand2());
            }
            equipmentItemWarningRule.setWarnLevel(eventTemplateItem.getLevel());
            if (eventTemplateItem.getRemarks() != null && !eventTemplateItem.getRemarks().equals("")) {
                equipmentItemWarningRule.setRemarks(eventTemplateItem.getRemarks());
            }
            this.save(equipmentItemWarningRule);
        }
    }

    Map<String, Map<String, String>> warningRuleMap() {
        Map<String, Map<String, String>> ruleMap = new HashMap<>();//存储所有触发的规则
        List<EquipmentItemWarningRule> recordList = dao.findList(new EquipmentItemWarningRule());
        for (EquipmentItemWarningRule warningRule : recordList) {
            String key = warningRule.getSgroupId() + "@" + warningRule.getItemId() + "@" + warningRule.getParam().getId();
            String value = warningRule.getComparisonOperator1() + "@" + warningRule.getOperand1() + "@" + warningRule.getRelationOperator() + "@" + warningRule.getComparisonOperator2() + "@" + warningRule.getOperand2() + "@" + warningRule.getWarnLevel() + "@" + warningRule.getRemarks();
            if (ruleMap.get(key) == null) {
                ruleMap.put(key, new HashMap<String, String>());
            }
            ruleMap.get(key).put(key + "@" + warningRule.getId(), value);
        }
//        System.out.println("ruleMap = " + ruleMap);
        return ruleMap;
    }

    @Transactional(readOnly = false)
    public void delete(EquipmentItemWarningRule equipmentItemWarningRule) {
        for (WarningRecord warningRecord : warningRecordService.findList(new WarningRecord(null, null, null, equipmentItemWarningRule.getId()))) {
            warningRecordService.delete(warningRecord);
        }
        super.delete(equipmentItemWarningRule);
    }
}
